package com.knife.bbs.business;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.knife.bbs.domain.Mail;
import com.knife.bbs.domain.WebMessage;

/**
 * 
 * <p>
 * Title:XML格式配置文件信息处理
 * </p>
 * <p>
 * Description:读取XML格式的KnifeBBS配置文件信息；
 * </p>
 * <p>
 * @author stef_wu
 */
public class XMLConfigFactory {
	private Document doc;

	private static final Logger logger = (Logger) Logger
			.getLogger(XMLConfigFactory.class.getName());

	public XMLConfigFactory() {
		synchronized (XMLConfigFactory.class) {
			InputStream in = null;
			try {				
				in = XMLConfigFactory.class
						.getResourceAsStream("/knife-bbsconfig.xml");
				SAXReader reader = new SAXReader();
				doc = reader.read(in);
				// System.out.println(doc.asXML());
			} catch (Exception e) {
				System.out.println("查找BBS配置文件文件错误！" + e);
			}

		}
	}

	public XMLConfigFactory(InputStream in) {
		try {
			SAXReader reader = new SAXReader();
			doc = reader.read(in);			
		} catch (Exception e) {
			logger.error("配置文件错误！" + e);
		}

	}

	public void initAdmin(List admins) {
		if (doc == null)
			return;
		admins.clear();
		List nodes = doc.selectNodes("/knife-bbsconfig/admins/admin");
		for (int i = 0; i < nodes.size(); i++) {
			Element e = (Element) nodes.get(i);
			admins.add(e.attributeValue("name"));
		}

	}

	public void initSystemMessage(Map message) {
		if (doc == null)
			return;
		message.clear();
		List nodes = doc.selectNodes("/knife-bbsconfig/messages/message");
		for (int i = 0; i < nodes.size(); i++) {
			Element e = (Element) nodes.get(i);
			WebMessage m = new WebMessage();
			m.setTitle(e.attributeValue("title"));
			m.setMessage(e.attributeValue("content"));
			m.setFromname(e.attributeValue("from"));
			message.put(e.attributeValue("name"), m);

		}
	}

	public String initTickTime() {
		if (doc == null)
			return null;
		String time = null;
		Node node = doc.selectSingleNode("/knife-bbsconfig/others/delay");
		if (node != null)
			time = ((Element) node).getText();
		return time;
	}

	public String initHostName() {
		if (doc == null)
			return null;
		String hostName = null;
		Node node = doc.selectSingleNode("/knife-bbsconfig/others/host");
		if (node != null)
			hostName = ((Element) node).getText();
		return hostName;
	}

	public void initMail(Map mails) {
		if (doc == null)
			return;
		List nodes = doc.selectNodes("/knife-bbsconfig/mails/mail");		
		if (nodes != null) {			
			for (int i = 0; i < nodes.size(); i++) {
				Element e = (Element) nodes.get(i);				
				Mail mail = createMail(e);
				mails.put(new Integer(i), mail);				
			}
		}
	}

	private Mail createMail(Element e) {	
		Mail mail = new Mail();
		mail.setCharset("utf-8");	
		try {
			mail.setMsg(e.attributeValue("content"));			
			mail.setFrom(e.attributeValue("from"));			
		} catch (Exception ex) {
			//System.out.println("配置文件错误！" + ex);
		}

		mail.setHost(e.attributeValue("host"));		
		mail.setSubject(e.attributeValue("subject"));		
		return mail;
	}

	public Document parse(String fileName) throws DocumentException {
		SAXReader reader = new SAXReader();
		File file = new File(fileName);
		Document document = file.exists() ? reader.read(fileName) : null;
		return document;
	}

}
